<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多功能音视频播放器</title>
    <style>
        :root {
            --primary-color: #007BFF;
            --hover-color: #0056b3;
            --text-color: #333;
            --bg-color: #f0f0f0;
            --card-bg: #fff;
            --shadow-color: rgba(0, 0, 0, 0.1);
        }
        
        .dark-mode {
            --primary-color: #58a6ff;
            --hover-color: #79b8ff;
            --text-color: #e1e1e1;
            --bg-color: #121212;
            --card-bg: #1e1e1e;
            --shadow-color: rgba(0, 0, 0, 0.3);
        }
        
        * {
            box-sizing: border-box;
            transition: background-color 0.3s, color 0.3s;
        }
        
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: var(--bg-color);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: var(--text-color);
            padding: 20px;
        }
        
        .player-container {
            background-color: var(--card-bg);
            padding: 25px;
            border-radius: 15px;
            box-shadow: 0 6px 12px var(--shadow-color);
            text-align: center;
            width: 90%;
            max-width: 800px;
        }
        
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        
        h1 {
            margin: 0;
            font-size: 1.8rem;
        }
        
        video, audio {
            max-width: 100%;
            border-radius: 10px;
            background-color: #000;
            margin-bottom: 15px;
            box-shadow: 0 4px 8px var(--shadow-color);
        }
        
        .controls {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 10px;
            margin-bottom: 20px;
        }
        
        button, .file-label {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }
        
        button:hover, .file-label:hover {
            background-color: var(--hover-color);
        }
        
        .theme-toggle {
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            color: var(--text-color);
            padding: 5px;
        }
        
        .playlist {
            margin-top: 20px;
            max-height: 200px;
            overflow-y: auto;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 10px;
            background-color: rgba(0,0,0,0.03);
            text-align: left;
        }
        
        .playlist-item {
            padding: 8px 12px;
            border-radius: 4px;
            margin-bottom: 5px;
            cursor: pointer;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .playlist-item:hover {
            background-color: rgba(0,123,255,0.1);
        }
        
        .playlist-item.active {
            background-color: rgba(0,123,255,0.2);
            font-weight: bold;
        }
        
        .playlist-item .remove-btn {
            font-size: 18px;
            color: #dc3545;
            background: none;
            border: none;
            padding: 0 5px;
            cursor: pointer;
        }
        
        .file-input {
            display: none;
        }
        
        @media (max-width: 768px) {
            .player-container {
                padding: 15px;
                width: 95%;
            }
            
            h1 {
                font-size: 1.5rem;
            }
            
            button, .file-label {
                padding: 8px 16px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="player-container">
        <div class="header">
            <h1>多功能音视频播放器</h1>
            <button class="theme-toggle" id="themeToggle">🌙</button>
        </div>
        
        <video id="videoPlayer" controls width="640" height="480"></video>
        <audio id="audioPlayer" controls></audio>
        
        <div class="controls">
            <label class="file-label" for="videoInput">
                <span>📹 打开视频</span>
                <input id="videoInput" type="file" accept="video/*" class="file-input">
            </label>
            
            <label class="file-label" for="audioInput">
                <span>🎵 打开音频</span>
                <input id="audioInput" type="file" accept="audio/*" class="file-input">
            </label>
            
            <button id="playlistBtn">📋 播放列表</button>
        </div>
        
        <div id="playlist" class="playlist" style="display: none;">
            <h3>播放列表</h3>
            <div id="playlistItems"></div>
        </div>
    </div>

    <script>
        const videoPlayer = document.getElementById('videoPlayer');
        const audioPlayer = document.getElementById('audioPlayer');
        const videoInput = document.getElementById('videoInput');
        const audioInput = document.getElementById('audioInput');
        const themeToggle = document.getElementById('themeToggle');
        const playlistBtn = document.getElementById('playlistBtn');
        const playlist = document.getElementById('playlist');
        const playlistItems = document.getElementById('playlistItems');
        
        // 播放列表数据
        const mediaFiles = {
            video: [],
            audio: []
        };
        let currentActiveType = null;
        let currentActiveIndex = -1;
        
        // 初始化
        function init() {
            // 初始设置
            audioPlayer.style.display = 'none';
            
            // 检查并应用已保存的主题
            if (localStorage.getItem('darkMode') === 'enabled') {
                document.body.classList.add('dark-mode');
                themeToggle.textContent = '☀️';
            }
            
            // 加载保存的播放列表
            loadPlaylist();
        }
        
        // 监听视频文件输入
        videoInput.addEventListener('change', function(event) {
            handleFileSelect(event, 'video');
        });
        
        // 监听音频文件输入
        audioInput.addEventListener('change', function(event) {
            handleFileSelect(event, 'audio');
        });
        
        // 处理文件选择
        function handleFileSelect(event, type) {
            const file = event.target.files[0];
            if (file) {
                // 添加到播放列表
                const url = URL.createObjectURL(file);
                mediaFiles[type].push({
                    name: file.name,
                    url: url
                });
                
                // 播放新添加的文件
                playMedia(type, mediaFiles[type].length - 1);
                
                // 更新播放列表UI
                updatePlaylistUI();
                
                // 保存播放列表
                savePlaylist();
                
                // 显示播放列表
                playlist.style.display = 'block';
            }
        }
        
        // 播放媒体
        function playMedia(type, index) {
            if (mediaFiles[type][index]) {
                // 设置当前活动类型和索引
                currentActiveType = type;
                currentActiveIndex = index;
                
                // 设置播放器显示状态
                videoPlayer.style.display = type === 'video' ? 'block' : 'none';
                audioPlayer.style.display = type === 'audio' ? 'block' : 'none';
                
                // 设置播放源并播放
                const player = type === 'video' ? videoPlayer : audioPlayer;
                player.src = mediaFiles[type][index].url;
                player.load();
                player.play();
                
                // 更新播放列表UI
                updatePlaylistUI();
            }
        }
        
        // 更新播放列表UI
        function updatePlaylistUI() {
            playlistItems.innerHTML = '';
            
            if (mediaFiles.video.length > 0) {
                const videoHeader = document.createElement('h4');
                videoHeader.textContent = '视频';
                playlistItems.appendChild(videoHeader);
                
                mediaFiles.video.forEach((item, index) => {
                    createPlaylistItem('video', item, index);
                });
            }
            
            if (mediaFiles.audio.length > 0) {
                const audioHeader = document.createElement('h4');
                audioHeader.textContent = '音频';
                playlistItems.appendChild(audioHeader);
                
                mediaFiles.audio.forEach((item, index) => {
                    createPlaylistItem('audio', item, index);
                });
            }
        }
        
        // 创建播放列表项
        function createPlaylistItem(type, item, index) {
            const itemElement = document.createElement('div');
            itemElement.className = 'playlist-item';
            if (currentActiveType === type && currentActiveIndex === index) {
                itemElement.classList.add('active');
            }
            
            const nameSpan = document.createElement('span');
            nameSpan.textContent = item.name;
            itemElement.appendChild(nameSpan);
            
            const removeBtn = document.createElement('button');
            removeBtn.className = 'remove-btn';
            removeBtn.innerHTML = '&times;';
            removeBtn.onclick = (e) => {
                e.stopPropagation();
                removePlaylistItem(type, index);
            };
            itemElement.appendChild(removeBtn);
            
            itemElement.addEventListener('click', () => {
                playMedia(type, index);
            });
            
            playlistItems.appendChild(itemElement);
        }
        
        // 移除播放列表项
        function removePlaylistItem(type, index) {
            mediaFiles[type].splice(index, 1);
            
            // 如果删除的是当前播放的项目
            if (currentActiveType === type && currentActiveIndex === index) {
                if (mediaFiles[type].length > 0) {
                    playMedia(type, 0);
                } else {
                    const player = type === 'video' ? videoPlayer : audioPlayer;
                    player.pause();
                    player.src = '';
                    player.style.display = 'none';
                    currentActiveType = null;
                    currentActiveIndex = -1;
                }
            } else if (currentActiveType === type && currentActiveIndex > index) {
                currentActiveIndex--;
            }
            
            updatePlaylistUI();
            savePlaylist();
        }
        
        // 播放列表按钮点击事件
        playlistBtn.addEventListener('click', function() {
            playlist.style.display = playlist.style.display === 'none' ? 'block' : 'none';
        });
        
        // 主题切换事件
        themeToggle.addEventListener('click', function() {
            if (document.body.classList.contains('dark-mode')) {
                document.body.classList.remove('dark-mode');
                localStorage.setItem('darkMode', 'disabled');
                themeToggle.textContent = '🌙';
            } else {
                document.body.classList.add('dark-mode');
                localStorage.setItem('darkMode', 'enabled');
                themeToggle.textContent = '☀️';
            }
        });
        
        // 保存播放列表到localStorage
        function savePlaylist() {
            // 简化版 - 只保存文件名，不保存URL（URL在页面刷新后会失效）
            const simplifiedList = {
                video: mediaFiles.video.map(item => item.name),
                audio: mediaFiles.audio.map(item => item.name)
            };
            localStorage.setItem('mediaPlaylist', JSON.stringify(simplifiedList));
        }
        
        // 从localStorage加载播放列表
        function loadPlaylist() {
            const saved = localStorage.getItem('mediaPlaylist');
            if (saved) {
                try {
                    const parsed = JSON.parse(saved);
                    // 注意：这里只恢复了名字，但URL已经失效
                    // 实际应用中可能需要更复杂的存储机制
                    updatePlaylistUI();
                } catch (e) {
                    console.error('加载播放列表失败', e);
                }
            }
        }
        
        // 播放结束时自动播放下一个
        videoPlayer.addEventListener('ended', () => playNext('video'));
        audioPlayer.addEventListener('ended', () => playNext('audio'));
        
        // 播放下一个
        function playNext(type) {
            if (currentActiveType === type && mediaFiles[type].length > currentActiveIndex + 1) {
                playMedia(type, currentActiveIndex + 1);
            }
        }
        
        // 初始化
        init();
    </script>
</body>
</html>